//课程相关的
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios, { AxiosResData } from '@/utils/http';
// import { AxiosRes } from '@/utils/http';
import { RootState } from '../index';
import { UserInfo, MenuData, getUserInfoRequest } from '@/api/request';
export type PageList<T = any> = {
  list: T[];
  count: number;
};

type Data = {
  menu: MenuData[];
  user_info: UserInfo;
  // student_list: UserInfo[]
  // student_list_count: number
  is_show_user_edit_modal: boolean;
  current_edit_userinfo: UserInfo;
  admin_list: UserInfo[];

  student_list_data: PageList<UserInfo>; // 数据列表 数据长度
  student_list_search_params: any; // 搜索条件
  student_list_current_page: number; // 页码
};

const initialState: Data = {
  menu: [],
  user_info: {} as UserInfo, //存储的用户信息
  is_show_user_edit_modal: false,
  current_edit_userinfo: {} as UserInfo,
  admin_list: [],
  student_list_data: {
    list: [],
    count: 0,
  },
  student_list_search_params: {},
  student_list_current_page: 1,
};

export const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    set_user_info: (state, action) => {
      state.user_info = action.payload;
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(get_user_info.fulfilled, (state, action) => {
        state.user_info = action.payload;
      })
      //改menu的状态
      .addCase(get_menu_async.fulfilled, (state, action) => {
        state.menu = action.payload;
      });
  },
});

//登录
export const get_user_info = createAsyncThunk<any>('get/user_info', async () => {
  //   const res: AxiosRes = await axios.get('/api/user');
  return await getUserInfoRequest();
});
//调菜单的接口
export const get_menu_async = createAsyncThunk('get/user_menu', async () => {
  const res: AxiosResData = await axios.get('/api/user/menu');
  return res.data.data;
});

export const select_user_info = (state: RootState) => {
  return state.user.user_info;
};
export const select_menu = (state: RootState) => {
  return state.user.menu;
};

export const { set_user_info } = userSlice.actions;
export default userSlice.reducer;
